<style lang="scss" scoped="">
  @import '../../styles/variables';


</style>

<template>
  <div>
    <div class="list-bar-tools">
      <div>
        <el-input placeholder="" v-model="filter.serviceId" class="list-bar-tools-input" size="mini"
                  suffix-icon="el-icon-search" clearable="">
          <template slot="prepend">服务Id</template>
        </el-input>
      </div>
    </div>


    <el-table
      :data="nodeList"
      border=""
      style="width: 100%">
      <el-table-column
        prop="serviceId"
        label="服务ID" width="380px">
      </el-table-column>
      <el-table-column
        sortable
        prop="success"
        label="成功次数">
      </el-table-column>
      <el-table-column
        sortable
        prop="failure"
        label="失败次数">
      </el-table-column>

      <el-table-column
        sortable
        prop="concurrent"
        label="最大并发数">
      </el-table-column>
      <el-table-column
        sortable
        prop="executeTimeLast"
        label="总执行时间">
      </el-table-column>
      <el-table-column
        sortable
        prop="executeTimeMin"
        label="最短执行时间">
      </el-table-column>
      <el-table-column
        sortable
        prop="executeTimeMax"
        label="最长执行时间">
      </el-table-column>
      <el-table-column
        sortable
        prop="avgTime"
        label="平均执行时间">
      </el-table-column>

    </el-table>

    <div class="pagination">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 50, 100]"
        :page-size="filter.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="nodeCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import utils from '@/utils/utils';
  import {nodeService} from '@/api/index';
  import {mapGetters} from 'vuex';

  export default {
    data() {
      return {
        filter: {
          pageNo: 1,
          pageSize: 10,
          start: 0,
          serviceId: '',
          nodeId: ''
        },
        nodeCount: 0,
        nodeList: [],
        serviceId: ''
      };
    },
    computed: {
      currentNodeId() {
        return this.$route.params.nodeId;
      }
    },
    watch: {
      'filter': {
        handler: function () {
          this.getNodeList();
        },
        deep: true
      }
    },
    components: {},
    methods: {
      handleCurrentChange(currentPageNo) {
        this.filter.start = (currentPageNo - 1) * this.filter.pageSize;
      },
      handleSizeChange(pageSize) {
        this.filter.pageSize = pageSize;
        localStorage.setItem('node-statics-page-size', pageSize);

      },
      getNodeList() {
        if (!this.currentNodeId) {
          return false;
        }
        nodeService.getStaticsData(this.filter).then(res => {
          this.nodeCount = res.data.total;
          res.data.rows.forEach(item => {
            item.avgTime = item.success ? (item.executeTimeLast / item.success) : 0;
            item.avgTime = Math.round(item.avgTime * 10000) / 10000;
          });
          this.nodeList = res.data.rows;
        });
      }
    },
    created() {
      let oldPageSize = localStorage.getItem('node-statics-page-size');
      if (!oldPageSize) {
        oldPageSize = 10;
      }
      this.filter.pageSize = Math.floor(oldPageSize);
    },
    mounted() {
      this.filter.nodeId = this.currentNodeId;
    }
  };
</script>
